package com.fhz.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    SQLiteDatabase db;
    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyOpenHelper myOpenHelper = new MyOpenHelper(this, "school.db", null, 1);

        //都可以读写-> 磁盘不够的时候,处理方式不同.写报错,读重试.
        db = myOpenHelper.getReadableDatabase();
//        myOpenHelper.getWritableDatabase();
    }

    public void add(View view) {
        //1 原生 sql
//        String sql = "insert into user(name,age) values (?,?)";  //开启
//        db.execSQL(sql,new Object[]{"小明",16}); //关闭


        //开启事务
        db.beginTransaction();
        //2 提供的方法
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","小小明");
        contentValues.put("age",17);
        db.insert("user",null,contentValues);

        //报错
//        int a = 0;
//        int b = 10 / a;

        ContentValues contentValues1 = new ContentValues();
        contentValues1.put("name","大大明");
        contentValues1.put("age",17);

        db.insert("user",null,contentValues1);

        //提交
        db.setTransactionSuccessful();

        db.endTransaction();


    }

    public void query(View view) {
        //1原生
//        String sql = "";
//        Cursor cursor = db.rawQuery(sql, null);
//        while (cursor.moveToNext()){
//            int name1 = cursor.getColumnIndex("name");
//            String name = cursor.getString(name1);
//            int age1 = cursor.getColumnIndex("age");
//            int age = cursor.getInt(age1);
//
//            Toast.makeText(MainActivity.this, name+""+age, Toast.LENGTH_SHORT).show();
//        }

        //2 方法
        //select name,age from user where id = 1
        Cursor cursor = db.query("user", new String[]{"name", "age"}, null, null, null, null, null);
        while (cursor.moveToNext()){
            int name1 = cursor.getColumnIndex("name");
            String name = cursor.getString(name1);
            int age1 = cursor.getColumnIndex("age");
            int age = cursor.getInt(age1);

//            Toast.makeText(MainActivity.this, name+""+age, Toast.LENGTH_SHORT).show();
            Log.i(TAG, "query: "+name+"年龄:"+age);
        }
    }

    public void del(View view) {
        //1原生
//        String sql = "delete from user where id = ?";
//        db.execSQL(sql,new Object[]{1});

        //2
        db.delete("user","id = ?",new String[]{"2"});
    }

    public void update(View view) {
        //1原生
//        String sql = "update user set name = ? where name = ?";
//        db.execSQL(sql,new Object[]{"haha","小明"});
        //2
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","小明");
        db.update("user",contentValues,"name = ?",new String[]{"haha"});
    }
}